package com.ys.erp.entity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ys.erp.entity.data.bean.WfbOrder;
import com.ys.erp.entity.mapper.WfbOrderMapper;
import com.ys.erp.entity.service.IWfbOrderService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lwj
 * @date 2025/6/20
 */
    
@Service
public class IWfbOrderServiceImpl extends ServiceImpl<WfbOrderMapper, WfbOrder> implements IWfbOrderService{

    @Override
    public List<WfbOrder> listUserIsNotNull() {
        List<WfbOrder> wbfOrderList = new ArrayList<>();
        queryUserIsNotNullLimit(wbfOrderList, 0, 800);
        return wbfOrderList;
    }

    public void queryUserIsNotNullLimit(List<WfbOrder> wfbOrderList, Integer start, Integer end) {
        QueryWrapper<WfbOrder> wfbOderQw = new QueryWrapper<>();
        wfbOderQw.lambda().isNotNull(WfbOrder::getUserId);
        wfbOderQw.lambda().isNotNull(WfbOrder::getCatCode);
        wfbOderQw.lambda().last(" LIMIT " + start + ", " + end);
        List<WfbOrder> list = this.list(wfbOderQw);
        wfbOrderList.addAll(list);
        if (wfbOrderList.size() == end+start) {
            queryUserIsNotNullLimit(wfbOrderList, end+start, 800);
        }
    }

    @Override
    public WfbOrder getByNo(String orderNo) {
        QueryWrapper<WfbOrder> wfbOrderQw = new QueryWrapper<>();
        wfbOrderQw.lambda().eq(WfbOrder::getWfbSubscriptionNo, orderNo);
        return this.getOne(wfbOrderQw);
    }

    @Override
    public List<WfbOrder> listByNo(Collection<String> noList) {
        if (noList.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<WfbOrder> orderQw = new QueryWrapper<>();
        orderQw.lambda().in(WfbOrder::getWfbSubscriptionNo, noList);
        return this.list(orderQw);
    }

    @Override
    public Map<String, WfbOrder> mapByNo(Collection<String> noList) {
        return listByNo(noList).stream().collect(Collectors.toMap(WfbOrder::getWfbSubscriptionNo, order -> order));
    }

    @Override
    public Map<String, WfbOrder> mapByUser(Collection<Long> userIdList) {
        return listByUser(userIdList).stream()
                .collect(Collectors.toMap(
                        WfbOrder::getOnlyUserNo,
                        it -> it
                ));
    }

    @Override
    public List<WfbOrder> listByUser(Collection<Long> userIdList) {
        QueryWrapper<WfbOrder> orderQw = new QueryWrapper<>();
        orderQw.lambda().in(WfbOrder::getUserId, userIdList);
        return this.list(orderQw);
    }

}
